<?php

class IRequest extends IObject implements ISingleton {
	private static $instance;
	private $params = array();
	
	private function __construct() {
		
	}
	
	/**
	 * Enter description here...
	 *
	 * @return IRequest
	 */
	public static function singleton() {
		if (self::$instance == null) {
			self::$instance = new self;
		}
		return self::$instance;
	}
	
	public function addParam($param, $value) {
		$this->params[$param] = $value;
	}
	
	/**
	 * Enter description here...
	 *
	 * @param unknown_type $name
	 * @return unknown
	 */
	public function param($name) {
		if (array_key_exists($name, $this->params)) {
			return $this->params[$name];
		}
		
		$post = $this->post($name);
		if ($post !== null) {
			return $post;
		}
		
		$get = $this->get($name);
		if ($get !== null) {
			return $get;
		}
		
		$cmdOptions = $this->cmdOptions();
		if (isset($cmdOptions["-" . $name])) {
			return $cmdOptions["-" . $name];
		}
		return null;
	}
	
	public function params() {
		$cmdOptions = $this->cmdOptions();
		$opts = array();
		foreach ($cmdOptions as $name=>$opt) {
			$opts[ltrim($name, "-")] = $opt;
		}
		$params = array_merge($opts, $this->gets());
		$params = array_merge($params, $this->posts());
		$params = array_merge($params, $this->params);
		return $params;
	}
	
	/**
     * 简洁的获取post方法提交过来的值
     * 
     * @param string $name 变量的值
     * @param mixed $default 如未设定，则设为此默认值
     * @return mixed 变量的值
     */
    public function post($name, $default = null) {
    	return (isset($_POST[$name]))?$this->strip($_POST[$name]):$default;	
    }
    
    /**
     * 简洁的获取get方法提交过来的值
     * 
     * @param string $name 变量的值
     * @param mixed $default 如未设定，则设为此默认值
     * @return mixed 变量的值
     */
    public function get($name, $default = null) {
    	return (isset($_GET[$name]))?$this->strip($_GET[$name]):$default;	
    }    
    
    /**
	 * 得到系统是否自动对传递的参数进行转义
	 * 
	 * @return boolean  
	 */
    public function getMagic() {
        return get_magic_quotes_gpc();
    }
    
   /**
	* 根据magic_quotes_gpc的值处理集合中的元素
	* 
	* 如果magic_quotes_gpc为1，则将一个集合中每个元素系统自动加的转义符去掉
	* 
	* @param string|array $values 集合
	* @return  string|array 结果
	*/
    private function strip($values) {
        if (is_array($values)) {
            foreach ($values as $k=>$v) {
                $values[$k] = $this->strip($v);
            }
        }
        else {
            if ($this->getMagic()) {
                $values = stripslashes($values);
            }
        }
        return $values;
    }
	
	/**
	 * 取得命令行传入的参数
	 * 
	 * <code>
	 * 只能接受
	 *   php cmd.php -k=ko -p=po
	 * 的参数，无法分析
	 * 	 php cmd.php -k ko -p po
	 * 要实现后一种命令行的分析，请使用util.cli
	 * </code>
	 *
	 * @return array
	 */
	public function cmdOptions() {
		if (!isset($_SERVER["argv"])) {
			return array();
		}
		$argv = $_SERVER["argv"];
		$options = array();
		if (!empty($argv)) {
			unset($argv[0]);
			foreach ($argv as $option) {
				if (strstr($option, "=")) {
					list($optionName, $optionValue) = explode("=", $option, 2);
					$options[$optionName] = $optionValue;
				}
				else {
					$options[$option] = $option;
				}
			}
		}
		return $options;
	}
	
	public function gets() {
		return $this->strip($_GET);
	}
	
	public function posts() {
		return $this->strip($_POST);
	}
	
  	/**
     * 得到当前客户端的IP
     * 
     * @return string 当前客户端的IP
     */
    public function ip() {
        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) {
            $ip = getenv("HTTP_CLIENT_IP");
        }
        elseif (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }
        elseif (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
            $ip = getenv("REMOTE_ADDR");
        }
        elseif (isset($_SERVER["REMOTE_ADDR"]) && $_SERVER["REMOTE_ADDR"] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
            $ip = $_SERVER["REMOTE_ADDR"];
        }
        else {
            $ip = "0.0.0.0";
        }
        return $ip;
    }
    
    public function method() {
    	if (isset($_SERVER["REQUEST_METHOD"])) {
        	return $_SERVER["REQUEST_METHOD"];
    	}
    	return null;
    }
    
    public function isCli() {
    	return isset($_SERVER["argv"]);
    }
    
    public function isGet() {
    	return $this->method() == "GET";
    }
    
    public function isPost() {
    	return $this->method() == "POST";
    }
    
    public function isPut() {
    	return $this->method() == "PUT";
    }
    
    public function script() {
    	if (isset($_SERVER["PHP_SELF"])) {
            return $_SERVER["PHP_SELF"];
        }
        if (isset($_SERVER["SCRIPT_NAME"])) {
            return $_SERVER["SCRIPT_NAME"];
        }
        return null;
    }
    
    public function uri() {
		if (isset($_SERVER["REQUEST_URI"])) {
            return $_SERVER["REQUEST_URI"];
        }
        return null;
    }
    
    /**
     * Enter description here...
     *
     * @return IURI
     */
    public function uriObj() {
    	return new IURI($this->uri());
    }
    
    public function input() {
    	$stream = fopen("php://input", "r");
		$input = "";
		if ($stream) {
			while (!feof($stream)) {
				$input .= fgets($stream);
			}
			fclose($stream);
		}
		return $input;
    }
    
    /**
     * Enter description here...
     * 
     * 不能重设命令行传入的参数
     *
     * @param unknown_type $param
     */
    public function remove($param) {
    	if (array_key_exists($param, $this->params)) {
    		unset($this->params[$param]);
    	}
    	if (array_key_exists($param, $_GET)) {
    		unset($_GET[$param]);
    	}
    	if (array_key_exists($param, $_POST)) {
    		unset($_POST[$param]);
    	}
    }
}


?>